<?php
// table_edit.php
// SuperConstructor Table Editor
// Edits an existing table in the database
// Copyright 2007, Alex McClung
require_once("../lib/func.autoloader.php");

// General variables
$t = NULL;

// If the user is not logged in, redirect them to the login page
$sc = new SuperConstructor();
if(false === $sc->getUser()) { header("Location: {$sc->config->LoginPage}"); exit(); }

// Has POST data been submitted?
// NOTE: This assumes the user has accepted the fact that they are going
//       to delete their table.
if(count($_POST) > 0)
{
	$tablename = mysql_escape_string($_POST['tablename']);
	
	// Try and delete the table.
	$t = new SimpleTable($tablename);
	if($t->Remove())
	{
		MQ::Push("Successfully removed <strong>$tablename</strong>.", MQ_OK);
		// TODO: Figure out a good way to hide the submit (Remove) button so that the user doesn't try to remove it a second time
	}
	else
	{
		MQ::Push("Error removing table: " . $t->LastError(), MQ_ERROR);
	}
}
// No POST data hase been submitted. Get the table info from the table
// specified in GET
else
{
	// Grab table name from GET
	// TODO: Test to make sure the specified table exists
	$name = $_GET['table'];
	
	// Have the SimpleTable do all the hard work
	$t = new SimpleTable($name);
}

// Make some nasty arrays for the form to use
$comments = $key = "";
$colnames = $coltypes = $collengths = $colattribs = $colnulls = $coldefaults = $colkeys = $colfulltexts = $colincrements = array();

foreach($t->columns as $column)
{
	     if( $column->primary_key ) { $key = "PRIMARY"; }
	else if( $column->unique      ) { $key = "UNIQUE";	}
	else if( $column->index       ) { $key = "INDEX";	}
	else							{ $key = "";		}
	
	$colnames[]      = $column->name;
	$coltypes[]      = strtoupper($column->type);
	$collengths[]    = strtoupper($column->fieldinfo);
	$colattribs[]    = strtoupper($column->attributes);
	$colnulls[]      = strtoupper($column->null);
	$coldefaults[]   = $column->default;
	$colkeys[]       = $key;
	$colfulltexts[]  = $column->fulltext;
	$colincrements[] = $column->auto_increment;
}

// Get the default form variables in there
$sc->assign("num_columns",          count($t->columns) );
$sc->assign_by_ref("tablename",     $t->name           );
$sc->assign_by_ref("comments",      $t->comments       );
$sc->assign_by_ref("colnames",      $colnames          );
$sc->assign_by_ref("coltypes",      $coltypes          );
$sc->assign_by_ref("collengths",    $collengths        );
$sc->assign_by_ref("colattribs",    $colattribs        );
$sc->assign_by_ref("colnulls",      $colnulls          );
$sc->assign_by_ref("coldefaults",   $coldefaults       );
$sc->assign_by_ref("colkeys",       $colkeys           );
$sc->assign_by_ref("colfulltexts",  $colfulltexts      );
$sc->assign_by_ref("colincrements", $colincrements     );

// Cleanup
unset($t, $column, $key, $name);
	
// Assign template vars
$sc->assign("pageTitle",       "Remove a Table");
$sc->assign("extraStyles",     array("common/css/te.css") );
$sc->assign("extraScripts",    array("common/js/te.js")   );

// TODO: Define these as constants somewhere (or properties of the Column class?)
$sc->assign("_column_types",   array("BIGINT","BINARY","BLOB","BOOL","CHAR","DATE","DATETIME","DECIMAL","DOUBLE","ENUM","FLOAT","INT","LONGBLOB","LONGTEXT","MEDIUMBLOB","MEDIUMINT","MEDIUMTEXT","SET","SMALLINT","TEXT","TIME","TIMESTAMP","TINYBLOB","TINYINT","TINYTEXT","VARCHAR","VARBINARY","YEAR"));
$sc->assign("_column_attribs", array("","UNSIGNED","UNSIGNED ZEROFILL","ON UPDATE CURRENT_TIMESTAMP") );
$sc->assign("_column_nulls",   array("NOT NULL", "NULL")  );
$sc->assign("_column_keys",    array("", "PRIMARY", "INDEX", "UNIQUE") );

// Display the template
$sc->display("admin/pages/table_editor/remove.tpl");
?>